//
//  CYSelectAmountView.m
//  CreditAddressBookEE
//
//  Created by LE on 16/1/26.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYSelectAmountView.h"

@interface CYSelectAmountView ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *level1Btn;
@property (weak, nonatomic) IBOutlet UIButton *level2Btn;
@property (weak, nonatomic) IBOutlet UIButton *level3Btn;

@property(nonatomic, copy) CYSelectedAmountBlock block;

@property(nonatomic, strong) UILabel *selectedLabel;
@property(nonatomic, strong) UIButton *selectedBtn;
@end

@implementation CYSelectAmountView

- (void)awakeFromNib {
    self.otherTF.delegate = self;
    [self.commitBtn addTarget:self action:@selector(commit:) forControlEvents:UIControlEventTouchUpInside];
    
//    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(levelTap1:)];
//    [self.level1Label addGestureRecognizer:tap1];
//    [self setupLayer:self.level1Label];
    [self setupLayer:self.level1Btn];
    [self setupLayer:self.level2Btn];
    [self setupLayer:self.level3Btn];
    //默认选中第一个
    [self setupSelectedLabel:self.level1Label button:self.level1Btn];
    
//    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(levelTap2:)];
//    [self.level2Label addGestureRecognizer:tap2];
//    [self setupLayer:self.level2Label];
//    
//    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(levelTap3:)];
//    [self.level3Label addGestureRecognizer:tap3];
//    [self setupLayer:self.level3Label];
}
/*!
 *  @brief  设置button的layer
 */
- (void)setupLayer:(UIButton *)label {
    label.layer.borderWidth = 1;
    label.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    label.layer.masksToBounds = YES;
    label.layer.cornerRadius = 5;
}
/*!
 *  @brief  选中button
 */
- (void)setupSelectedLabel:(UILabel *)label button:(UIButton *)btn {
    self.selectedBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    self.selectedLabel = label;
    self.selectedBtn = btn;
    btn.layer.borderColor = CYNaviThemeColor.CGColor;
}
- (IBAction)level1Btn:(UIButton *)sender {
    [self.otherTF resignFirstResponder];
    [self setupSelectedLabel:self.level1Label button:sender];
}
- (IBAction)level2Btn:(id)sender {
    [self.otherTF resignFirstResponder];
    [self setupSelectedLabel:self.level2Label button:sender];
}
- (IBAction)level3Btn:(id)sender {
    [self.otherTF resignFirstResponder];
    [self setupSelectedLabel:self.level3Label button:sender];
}

//- (void)levelTap1:(UITapGestureRecognizer *)gr {
//    [self.otherTF resignFirstResponder];
//    [self setupSelectedLabel:self.level1Label];
//}
//
//- (void)levelTap2:(UITapGestureRecognizer *)gr {
//    [self.otherTF resignFirstResponder];
//    [self setupSelectedLabel:self.level2Label];
//    
//}
//
//- (void)levelTap3:(UITapGestureRecognizer *)gr {
//    [self.otherTF resignFirstResponder];
//    [self setupSelectedLabel:self.level3Label];
//    
//}

#pragma mark - 
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.selectedBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    self.selectedLabel = nil;
    self.otherTF.layer.borderColor = CYNaviThemeColor.CGColor;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.selectedBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    self.selectedLabel = nil;
    self.otherTF.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
}

#pragma mark -
- (void)commit:(UIButton *)btn {
    [self.otherTF resignFirstResponder];
    CGFloat ammount = 0;
    @try {
        if (self.selectedLabel) {
            ammount = self.selectedLabel.text.integerValue;
        } else if (self.otherTF.text && !self.otherTF.text.isEmpty) {
            ammount = self.otherTF.text.integerValue;
        }
    }
    @catch (NSException *exception) {
        CYLog(@"金额输入有误");
    }
    if (ammount > 0) {
        if (self.block) {
            self.block(ammount);
            self.block = nil;
        }
    } else {
        if (self.block) {
            self.block(ammount);
        }
    }
}

- (void)addTarget:(id)target action:(SEL)action {
    [self.commitBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)commitButtonClick:(CYSelectedAmountBlock)block {
    self.block = [block copy];
}
@end
